<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class ClockSetting extends \app\BaseModel
{
    protected $name = "massage_add_clock_setting";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function getCoachBalance($order, $coach_balance = 0)
    {
        $order_model = new Order();
        $add_order = $order_model->where(["pay_type" => 1, "add_pid" => $order["add_pid"], "is_add" => 1])->where("id", "<>", $order["id"])->select()->toArray();
        if(!empty($add_order)) {
            foreach ($add_order as $value) {
                $order_model->cancelOrder($value);
            }
        }
        if(empty($order["coach_id"])) {
            return $order["coach_balance"];
        }
        if($order["is_add"] != 1) {
            return $order["coach_balance"];
        }
        $config_model = new MassageConfig();
        if(!empty($order["admin_id"])) {
            $admin_model = new Admin();
            $admin_clock_cash_status = $admin_model->where(["id" => $order["admin_id"]])->value("clock_cash_status");
            $clock_cash_type = $admin_model->where(["id" => $order["admin_id"]])->value("clock_cash_type");
        }
        $times = $order_model->addOrderTimes($order["add_pid"]);
        if(!empty($admin_clock_cash_status) && $admin_clock_cash_status == 1) {
            $balance = $this->where(["uniacid" => $order["uniacid"], "admin_id" => $order["admin_id"], "type" => $clock_cash_type])->where("times", "<=", $times)->order("times desc,id desc")->find();
            if(!empty($balance)) {
                if($clock_cash_type == 1) {
                    $balance->balance = $balance->balance + $coach_balance;
                }
                return 100 < $balance->balance ? 100 : $balance->balance;
            }
        }
        $config = $config_model->dataInfo(["uniacid" => $order["uniacid"]]);
        if($config["clock_cash_status"] != 1) {
            return $order["coach_balance"];
        }
        $balance = $this->where(["uniacid" => $order["uniacid"], "admin_id" => 0, "type" => $config["clock_cash_type"]])->where("times", "<=", $times)->order("times desc,id desc")->find();
        if(empty($balance)) {
            return $order["coach_balance"];
        }
        $balance = $balance->toArray();
        $balance = !empty($balance) ? $balance["balance"] : 0;
        if($config["clock_cash_type"] == 1) {
            $balance = $balance + $coach_balance;
        }
        return 100 < $balance ? 100 : $balance;
    }
}

?>